package com.lunchEveryDay.dao;

import com.lunchEveryDay.entity.LunchSchedule;
import com.lunchEveryDay.entity.User;
import com.lunchEveryDay.entity.UserLunch;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by Admin on 2015/7/21.
 */
@Repository
public class UserLunchDao extends BaseDao<UserLunch,Long>{

	public List<UserLunch> getUserLunchByUser(User user){
		List<UserLunch> userLunchs = (List<UserLunch>)getCurrentSession().createQuery(
				  "from UserLunch as ul where ul.user = ?")
				  .setEntity(0, user)
				  .list();
		return userLunchs;
	}
	public List<LunchSchedule> getLunchScheduleByUser(User user){
		List<LunchSchedule> LunchSchedules = (List<LunchSchedule>)getCurrentSession().createQuery(
				  "from LunchSchedule as ls where ls.id= any(select ul.lunchSchedule from UserLunch as ul where ul.user = :user)")
				  .setEntity("user", user)
				  .list();
		return LunchSchedules;
	}

	public UserLunch getUserLunchByUserAndLunchSchedule(User user, LunchSchedule lunchSchedule) {
		return (UserLunch)getCurrentSession().createQuery(
				  "from UserLunch as ul where ul.user = :user and ul.lunchSchedule=:lunchSchedule")
				  .setEntity("user", user)
				  .setEntity("lunchSchedule", lunchSchedule)
				  .uniqueResult();
	}
}
